#!/usr/bin/env python
import sys
import os

#generate a tag file based on the two groups inserted into the function
def createTag(treatment, control, projectName):

	#Experiment parameters
	cutoff="0.0"
	methylation="True"
	arrayType="agilent"
	tpmapFolder="/media/Storage/Joseph/Documents/Research/SlamUsers/concatenated_data"
	normalization="separate" #separate=SLAM normalization
	dataFolder="/media/Storage/Joseph/Documents/Research/SlamUsers/concatenated_data"
	smoothingFactor="300"

	#print out tag file
	cwd = os.getcwd()
	fileName = cwd+"/tags/"+projectName+".tag"
	f1 = open(fileName,'w')
	f1.write("[project]\n")
	f1.write("1 = "+projectName+"\n\n")
	f1.write("[inputChannels]\n")
	f1.write("1 = "+treatment[-1]+control[-1]+"\n\n")
	f1.write("[tpmap]\n")
	f1.write("1 = zebrafish_chip1+2.tpmap\n\n")
	f1.write("[data]\n")

	#define data files to be used in experiment
	dataFiles = ""
	for n in treatment:
		if n!=treatment[-1]:
			dataFiles = dataFiles+n+".txt "
	for n in control:
		if n!=control[-1]:
			dataFiles = dataFiles+n+".txt "
	f1.write("1 = "+dataFiles+"\n\n")
	f1.write("[settings]\n")
	f1.write("cutoff = "+cutoff+"\n")

	#define group 1=treatment 0=control
	group = ""
	for n in treatment:
		if n!=treatment[-1]:
			group = group + '1'
	for n in control:
		if n!=control[-1]:
			group = group + '0'

	f1.write("group = "+group+"\n")	# 1=treatment 0=control
	f1.write("methylation = "+methylation+"\n")
	f1.write("arraytype = "+arrayType+"\n")
	f1.write("tpmapfolder = "+tpmapFolder+"\n")
	f1.write("normalization = "+normalization+"\n")
	f1.write("datafolder = "+dataFolder+"\n")
	f1.write("lambda = "+smoothingFactor+"\n")

	f1.close()
	return


if __name__=="__main__":
	#Group definitions red=0 green=1 
	#Red or green specification must be the last element in the set (spaces are required)
	hulk = ["6472E1+2","6472E1+2","6472E3+4","6245E2+4","6195E1+2","0 1 1 1 1 "] 
	Input = ["6472E7+8","6245E1+3","6195E3+4","6111E1+2","0 1 1 0 "]
	shrek = ["6472E3+4", "6472E5+6", "0 1 "]
	srk31 = ["6472E3+4", "0 "]
	srk32 = ["6472E5+6", "1 "]
	otg = ["6472E5+6", "0 "] #21.1 Oscar the Grouch
	immatureB = ["6195E3+4", "0 "]
	immatureT = ["6245E2+4", "6195E1+2", "0 0 "]
	muscle = ["6272E7+8", "6148E1", "6148E1", "6111E1+2", "1 0 1 1 "]
	matureB = "6245E1+3" "0 "
	hlk81 = "6245E2+4" "6195E1+2" "1 1 " #TL1-1 and TL1-2 (not including stripped slides)
	hlk83 = "6472E1+2" "1 "
	hlk84 = "6472E1+2" "0 "
	hlk85 = "6472E3+4" "1 "
	treatmentGroups = [hulk, shrek ,srk31 ,srk32 ,otg , immatureB, immatureT, muscle, matureB, hlk81, hlk83, hlk84, hlk85]
	groupNames = ["hulk", "shrek", "srk31", "srk32", "otg", "immatureB",  "immatureT", "muscle", "matureB", "hlk81", "hlk83", "hlk84", "hlk85"]
	
	
	projectName = ""
	#Run each group vs Input
	for i, group in treatmentGroups:
		projectName = groupNames[i]+"_input"
		createTag(group,Input,projectName)
		os.system("python SLAM.py tags/"+projectName+".tag")

	#Run each group vs immatureT


